package Base.多线程同步.线程安全;

/**
 * @author 陈鹏
 * 使用sleep进行延时操作，但是还是会出现问题
 */
public class Example11 {
    public static void main(String[] args) {
        TicketWindow ticketWindow = new TicketWindow();
        Thread thread1 = new Thread(ticketWindow, "thread1");
        Thread thread2 = new Thread(ticketWindow, "thread2");
        Thread thread3 = new Thread(ticketWindow, "thread3");
        Thread thread4 = new Thread(ticketWindow, "thread4");
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
    }
}

class TicketWindow implements Runnable {
    private int tickets = 100;

    @Override
    public void run() {
        while (true) {
            if (tickets > 0) {
                try {
                    Thread.sleep(100);
                }catch (Exception e){

                }
                System.out.println(Thread.currentThread().getName() + "出售第" + tickets-- + "张票");
            }
        }
    }
}
